<?php

namespace Home\Controller;

use Common\Controller\CommonController;
use Common\Logic\TradeLogic;
use Home\Model\OrdersModel;
use Think\Model;

class OrdersController extends CommonController {
	public function _initialize() {
		parent::_initialize ();
	}
	// 交易页面显示
	public function index() {
		
		/* if (empty ( $_GET ['currency'] )) {
			$this->display ( 'Public:b_stop' );
			return;
		} */
		
		$token=I('token');
		
		if($token!=null){
			$where1['token']=$token;
			$member_id=M('token')->where($where1)->find()['uid'];
		}
		$currency_id = I ( 'currency' );
		$currency = M ( 'Currency' )->where ( "currency_mark='$currency_id' and is_line=1" )->find ();
		
		if (empty ( $currency )) {
			$this->display ( 'Public:b_stop' );
			return;
		}
		$currency ['currency_digit_num'] = $currency ['currency_digit_num'] ? $currency ['currency_digit_num'] : 0; // 设置限制位数
		                                                                                                   // 显示委托记录
		$buy_record = $this->getOrdersByType ( $currency ['currency_id'], 'buy', 5, 'desc' );
		$sell_record = $this->getOrdersByType ( $currency ['currency_id'], 'sell', 5, 'asc' );
		
		$data['buy_record']=$buy_record;
		$data['sell_record']=$sell_record;
		
		
		// 格式化手续费
		$currency ['currency_sell_fee'] = floatval ( $currency ['currency_sell_fee'] );
		$currency ['currency_buy_fee'] = floatval ( $currency ['currency_buy_fee'] );
		// 币种信息
		// $currency_message=$this->getCurrencyMessageById($currency['currency_id']);
		// //调用买一卖一方法去写
		// $min_order =$this->getOneOrdersByPrice($currency['currency_id'], 'sell');
		// $max_order =$this->getOneOrdersByPrice($currency['currency_id'], 'buy');
		// $currency_message['buy_one_price'] = $max_order;
		// $currency_message['sell_one_price'] = $min_order;
		// $currency_trade=$this->getCurrencynameById($currency['trade_currency_id']);
		// //总成交额 从文件里读取
		// if(S('Trade_allnum_'.$currency['currency_id'])){
		// $currency_message['trade_total'] = S('Trade_allnum_'.$currency['currency_id']);
		// }else{
		// $trade_total = M('Trade_'.$currency['currency_id'])->where("currency_id={$currency['currency_id']}")->sum('money');
		// $currency_message['trade_total'] = number_format($trade_total/2,3);
		// S('Trade_allnum_'.$currency_id,$currency_message);
		// }
		// 涨跌停查询
		$price_time = strtotime ( date ( 'Y-m-d', time () ) );
		$count_price ['add_time'] = array (
				'lt',
				$price_time 
		);
		$count_price ['currency_id'] = $currency ['currency_id'];
		$price_last = M ( 'Trade_' . $currency ['currency_id'] )->Field ( 'price' )->where ( $count_price )->order ( 'add_time desc' )->find ();
		$currencys = $this->getCurrencyByCurrencyId ( $currency ['currency_id'] );
		
		$down = ((1 - $currencys ['price_down'] / 100) * $price_last ['price']);
		$up = ((1 + $currencys ['price_up'] / 100) * $price_last ['price']);
		
		
		// $this->assign('currency_message',$currency_message);
		// $this->assign('currency_trade',$currency_trade);
		$TLogin = new TradeLogic ( $currency ['currency_id'] );
		$tlfield = "type,price,num,add_time";
		// 个人账户资产
		if (! empty ( $member_id )) {
			// 个人资金优化 ，getuserallmoney
			
			$user_info_money = $this->getuserallmoney ( $member_id,$currency ['currency_id'] );
			// dump($user_info_money);die();
			$user_currency_money ['currency'] ['num'] = $user_info_money ['num'];
			$user_currency_money ['currency'] ['forzen_num'] = $user_info_money ['forzen_num'];
			$user_currency_money ['currency'] ['all_num'] = $user_currency_money ['currency'] ['num'] + $user_currency_money ['currency'] ['forzen_num'];
			$user_info_trade_money = $this->getuserallmoney ( $currency ['trade_currency_id'] );
			$user_currency_money ['currency_trade'] ['num'] = $user_info_trade_money ['num'];
			$user_currency_money ['currency_trade'] ['forzen_num'] = $user_info_trade_money ['forzen_num'];
			$user_currency_money ['currency_trade'] ['all_num'] = $user_currency_money ['currency_trade'] ['num'] + $user_currency_money ['currency_trade'] ['forzen_num'];
			if ($currency ['trade_currency_id'] == 0) {
				$user_currency_money ['currency_trade'] ['num'] = $this->member ['rmb'];
				$user_currency_money ['currency_trade'] ['forzen_num'] = $this->member ['forzen_rmb'];
			}
			
			//--------------new--↓-------//
			foreach ($this->currency as $vo){
				$all_num = $this->getuserallmoney($vo['currency_id']);
				$price = $this->getCurrencyMessageById ( $vo ['currency_id'] );
				$all_num['price']  = $price['new_price']?$price['new_price']:0;
				$my_all_list[] = $all_num;
			}
			$shizhi = 0;
			foreach ($my_all_list  as $vo){
				$shizhi+=formatOrderNum($vo['num']*$vo['price']);
			}
			$user_currency_money['currency_trade']['cysz'] = $shizhi;
			//--------------new--↑-------//
			$data['user_currency_money']=$user_currency_money;
			
			// 个人挂单记录
			$user_orders=$this->getOrdersByUser($member_id,'', $currency ['currency_id'] );
			
			$data['user_orders']=$user_orders;
			
			// 最大可买
			if (! empty ( $sell_record )) {
				$buy_num = sprintf ( '%.4f', $user_currency_money ['currency_trade'] ['num'] / $sell_record [0] ['price'] );
			} else {
				$buy_num = 0;
			}
			$data['buy_num']=$buy_num;
			
			// 个人成交记录
			$mytwhere ['member_id'] =$member_id;
			$chengjiao = $TLogin->getTradeALLByUid ( $tlfield, 10, $mytwhere );
			$data['chengjiao']=$chengjiao;
			
			// 最大可卖
			$sell_num = sprintf ( '%.4f', $user_currency_money ['currency_trade'] ['num'] );
			// dump($user_currency_money);
			// die();
			$data['sell_num']=$sell_num;
			
		}
		//$this->assign ( 'session', $_SESSION ['member_id'] );
		// 成交记录
		
		// 这块是有问题的，最新成交记录里，还要带上部分成交的
		$trade = $TLogin->getTradeALLByUid ( $tlfield, 30 );
		
		$data['trade']=$trade;
		
		// dump($currency);
		// die();
		// 币种详情
	 	$currency_or = $this->getOrdersDataList();
	 	foreach ($currency_or as $v){
	 		if($v['currency_id'] == $currency['currency_id']){
	 			$currency_ors = $v;
	 		}
	 	}
	 	$data['currency']=$currency;
	 	$data['currency_ors']=$currency_ors;
		$where="member_id = '$member_id' and (num>0 or forzen_num >0)";
		$currency_user = M ( 'Currency_user' )
							->alias('C_U')
							->field ('C_U.*,C_U.num+C_U.forzen_num AS count,C.currency_name,C.currency_mark' )
							->join ("__CURRENCY__ AS C on C.currency_id= C_U.currency_id" )
							->where ( $where )->order ( 'sort' )->select ();
		foreach ( $currency_user as $k => $v ) {
			$x = M ( 'Trade_' . $v ['currency_id'] )->order ( "trade_id desc" )->find ();
			$list = $this->getCurrencyMessageById ( $v ['currency_id'] );
			// 市值
// 			$currency_user [$k] ['shizhi'] = ($v ['num'] + $v ['forzen_num']) * $x ['price'];
			// 持有均价
			$t = M ( "Trade_" . $v ['currency_id'] )
						->where ( "member_id = '$member_id'" )
						->field ( "avg(price) as price" )
						->select();
			$currency_user [$k] ['chiyoujunjia'] = formatOrderNum($t[0]['price']); // *$v['num']
			$currency_user [$k] ['num'] = $v['count'];
			$currency_user [$k] ['shizhi'] = formatOrderNum(($list ['new_price'] ? $list ['new_price'] : 0)*$currency_user [$k]['num']);
			$currency_user [$k] ['yk']  = formatOrderNum($list['new_price'] - $currency_user [$k] ['chiyoujunjia']);
			$currency_user [$k] ['ykbl']  =  formatOrderNum(($currency_user [$k] ['yk']/$currency_user [$k] ['chiyoujunjia'])*100);
		}
		// dump($currency_user);
		//$this->assign ( "currency_user", $currency_user );
		$data['currency_user']=$currency_user;
		//$this->ajaxReturn($currency_user,'JSONP');
		// 撤单
		$ORDERS = new OrdersModel ();
		$sell_order = $ORDERS->getAllByMemberIdAndTypeAndStatus ( $currency ['currency_id'], $_SESSION ['member_id'], 'sell', - 1 ); // 撤单卖单
		$buy_order = $ORDERS->getAllByMemberIdAndTypeAndStatus ( $currency ['currency_id'], $_SESSION ['member_id'], 'buy', - 1 ); // 撤单卖单
		$data['sell_order']=$sell_order;
		$data['buy_order']=$buy_order;
		//dump($data);
		$this->ajaxReturn($data,'JSONP');                                                                                                                // dump($buy_order);
		//$this->assign ( "sell_order", $sell_order );
		//$this->assign ( "buy_order", $buy_order );
		//$this->display ();
	}
	
	// 交易大厅
	public function currency_trade() {
		$count = M ( 'Currency' )->where ( 'is_line=1' )->count (); // 根据分类查找数据数量
		$page = new \Think\Page ( $count, 10 ); // 实例化分页类，传入总记录数和每页显示数
		$show = $page->show (); // 分页显示输出性
		$currency = M ( 'Currency' )->where ( 'is_line=1' )->order ( 'sort' )->limit ( $page->firstRow . ',' . $page->listRows )->select (); // 时间降序排列，越接近当前时间越高
		foreach ( $currency as $k => $v ) {
			$list = $this->getCurrencyMessageById ( $v ['currency_id'] );
			$currency [$k] = array_merge ( $list, $currency [$k] );
			$trade_total = M ( 'Trade_' . $v ['currency_id'] )->where ( "currency_id={$v['currency_id']}" )->sum ( 'money' );
			$currency [$k] ['currency_trade_all'] = number_format ( $trade_total, 2 );
			
			$list = $this->getCurrencyMessageById ( $v ['currency_id'] );
			$currency [$k] = array_merge ( $list, $currency [$k] );
			$list ['new_price'] ? $list ['new_price'] : 0;
			$currency [$k] ['currency_all_money'] = number_format ( floatval ( $v ['currency_all_num'] ) * $list ['new_price'], 2 );
		}
		$this->assign ( 'page', $show );
		$this->assign ( 'currency', $currency );
		$this->display ();
	}
	
	// 获取挂单记录
	public function getOrders() {
		switch (I ( 'post.type' )) {
			case 'buy' :
				$this->ajaxReturn ( $this->getOrdersByType ( I ( 'post.currency_id' ), 'buy', 3, 'desc' ) );
				break;
			case 'sell' :
				$this->ajaxReturn ( $this->getOrdersByType ( I ( 'post.currency_id' ), 'sell', 3, 'asc' ) );
				break;
		}
	}
	public function chicang(){
		$token=I('token');
		$where1['token']=$token;
		$member_id=M('token')->where($where1)->find()['uid'];
		//总充值金额
		$where2['member_id']=$member_id;
		$where2['status']=1;
		$pay=M('Pay')->where($where2)->select();
		$pay_money=0;
		foreach ($pay as $k=>$v){ 
			$pay_money+=$v['money'];
		}
		//挂单金额
		$where3['member_id']=$member_id;
		$where3['status']=array('in','0,1');
		$where3['type']='buy';
		$order=M('Orders')->where($where3)->select();
		$order_money=0;
		foreach ($order as $k=>$v){
			$order_money+=($v['num']-$v['trade_num'])*$v['price'];
			
		}
		//交易金额
		$trade_buy=0;
		$trade_sell=0;
		$where_buy=" where member_id = ".$member_id." and type = 'buy' ";
		$sql=$this->getTradeSql($where_buy);
		$Model=new Model();
		//$sql1 = $sql." where member_id = ".$member_id." and type = 'buy' ";
		$r=$Model->query($sql);
		foreach($r as $k=>$v){
			$trade_buy+=$v['money'];
			/* $where['currency_id']=$v['currency_id'];
			$currency_name=M('Currency')->where($where)->find();
			$trade=M('Trade_'.$v['currency_id'])->order('add_time desc')->find();
			$r[$k]['buy_price']=$v['price'];
			$r[$k]['new_price']=$trade['price'];
			$r[$k]['currency_name']=$currency_name['currency_name'];
			$r[$k]['all_num']=$v['num'];
			$r[$k]['fudongprice']=($trade['price']-$v['price'])*$v['num'];
			$r[$k]['fudongbfb']=(($trade['price']-$v['price'])*100/$v['price'])*$v['num']; */
		}
		$where_sell=" where member_id = ".$member_id." and type = 'sell' ";
		//$sql2= $sql." where member_id = ".$member_id." and type = 'sell' ";
		$sql2=$this->getTradeSql($where_sell);
		$r2=$Model->query($sql2);
		foreach ($r2 as $k=>$v){
			$trade_sell+=$v['money'];
		}
		//账户余额
		$where4['member_id']=$member_id;
		$member=M('Member')->where($where4)->find();
		$member_money=$member['num']+$member['forzen_num'];
		$all_profit=$member_money+$order_money+$trade_sell-$trade_buy-$pay_money;
		$currency=M('Currency')->where('is_line = 1')->select();
		$chicang=0;
		$buy_avg=0;
		$buy_times=0;
		foreach ($currency as $k =>$v){
			$where['member_id']=$member_id;
			$where['currency_id']=$v['currency_id'];
			$currency_user=M('Currency_user')->where($where)->find();

			$chicang=$currency_user['num'];
			$currency[$k]['chicang']=$currency_user['num'];
			$currency[$k]['guadan']=$currency_user['forzen_num'];
			$user_buy=M('Trade_'.$v['currency_id'])->where("member_id = ".$member_id." and type= 'buy'")->order('add_time desc')->select();
			$user_buyall=0;	
			$new_price=M('Trade_'.$v['currency_id'])->order('add_time desc')->find()['price'];
			foreach ($user_buy as $kk =>$v){
				$user_buyall+=$v['money'];
			}
			foreach ($user_buy as $kk =>$v){
				if($chicang>0){
				if($chicang>$v['num']){
					$buy_avg+=$v['price'];
					$buy_times++;
					$chicang=$chicang-$v['num'];
				}else{
					$buy_avg+=$v['price'];
					$buy_times++;
					$buyavg=$buy_avg/$buy_times;
					$currency[$k]['buyavg']=$buyavg;
					$currency[$k]['profit']=round(($new_price-$buyavg)*100/$buyavg,2);
					break;
				}
			}else{
				$currency[$k]['buyavg']=0;
				$currency[$k]['profit']=0;
			}
		}
			$user_sell=M('Trade_'.$v['currency_id'])->where("member_id = ".$member_id." and type= 'sell'")->select();
			$user_sellall=0;
			foreach ($user_sell as $kkk =>$v){
				$user_sellall+=$v['money'];
			}
			//dump($user_buyall);dump($user_sellall);
			$currency[$k]['profit_all']=$user_sellall-$user_buyall;
			$currency[$k]['new_price']=$new_price;
			
		}
		//
		$data['list']=$currency;
		$data['profit']=$all_profit;
		$this->ajaxReturn($data,'JSONP');
	/* 	foreach($r as $k=>&$v){
			if(!in_array($v['currency_id'], $currency_id)){
			
			$where['currency_id']=$v['currency_id'];
			$currency_mark=M('Currency')->where($where)->find();
			$v['currency_mark'] = $currency_mark['currency_mark'];
			$trade=M('Trade_'.$v['currency_id'])->order('price desc')->find();
			$v['new_price']=$trade['price'];
			$r1[$v['currency_id']]['buy_price']=$v['num']*$v['price'];
			$r1[$v['currency_id']]['new_price']=$trade['price'];
			$r1[$v['currency_id']]['currency_mark']=$currency_mark['currency_mark'];
			$r1[$v['currency_id']]['all_num']=$v['num'];
			$r1[$v['currency_id']]['fudongprice']=($trade['price']-$v['price'])*$v['num'];
			$r1[$v['currency_id']]['fudongbfb']=(($trade['price']-$v['price'])*100/$v['price'])*$v['num'];
			$currency_id[]=$v['currency_id'];
			
			}else{
				$r1[$v['currency_id']]['buy_price']+=$v['num']*$v['price'];
				$r1[$v['currency_id']]['all_num']+=$v['num'];
				$r1[$v['currency_id']]['fudongprice']+=($trade['price']-$v['price'])*$v['num'];
				$r1[$v['currency_id']]['fudongbfb']+=(($trade['price']-$v['price'])*100/$v['price'])*$v['num'];
			}
			
			
			
			
		} */
		/* foreach ($r as $k=>$v){
			$where['currency_id']=$v['currency_id'];
			$currency_mark=M('Currency')->where($where)->find();
			$r[$k]['currency_mark'] = $currency_mark['currency_mark'];
			$trade=M('Trade_'.$v['currency_id'])->order('price desc')->find();
			$r[$k]['new_price']=$trade['price'];
			$r[$k]['zhangfu']=($trade['price']-$v['price'])*100/$v['price'];
		} */
		//dump($r);dump($r1);die;
	}
	public function cancel(){
		$token=I('token');
		$where1['token']=$token;
		$member_id=M('token')->where($where1)->find()['uid'];
		$orders_id=I('orders_id');		
		$one_order=$this->getOneOrdersByMemberAndOrderId($member_id, $orders_id,array(0,1));
		if(empty($one_order)){
			$data['status'] = 0;
			$data['info'] = '传入信息错误';
			$this ->ajaxReturn($data,'JSONP');
		}
		$info = $this ->cancelOrdersByOrderId($one_order);
		if($info){
			$data['status']=1;
			$data['info']="操作成功";
			$this->ajaxReturn($data,'JSONP');
		}
		$data['status']=0;
		$data['info']="操作失败";
		$this->ajaxReturn($data,'JSONP');
	}
	/**
	 * 返回指定用户挂单记录
	 * @param int $member_id
	 * @param int $order_id
	 * @param array $status
	 */
	protected function getOneOrdersByMemberAndOrderId($member_id,$order_id,$status=array(0,1,2,-1)){
		$where['member_id']=$member_id;
		$where['orders_id']=$order_id;
		$where['status']=array('in',$status);
		$one_order=M('Orders')->where($where)->find();
		return  $one_order;
	}
	/**
	 *撤销订单
	 * @param   int $list  订货单信息
	 * @param   int $member_id  用户id
	 * @param   int $order_id  订单号 id
	 */
	protected  function cancelOrdersByOrderId($one_order){
		M()->startTrans();
		$r[]=$this->setOrdersStatusByOrdersId(-1, $one_order['orders_id']);
		//返还资金
		switch ($one_order['type']){
			case 'buy':
				$money=($one_order['num']-$one_order['trade_num'])*$one_order['price'];
				$r[]= $this->setUserMoney($one_order['member_id'], $one_order['currency_trade_id'],$money , 'inc', 'num');
				$r[]=$this->setUserMoney($one_order['member_id'], $one_order['currency_trade_id'], $money, 'dec', 'forzen_num');
				break;
			case 'sell':
				$num=$one_order['num']-$one_order['trade_num'] ;
				$r[]= $this->setUserMoney($one_order['member_id'], $one_order['currency_id'],$num, 'inc', 'num');
				$r[]=$this->setUserMoney($one_order['member_id'], $one_order['currency_id'], $num, 'dec', 'forzen_num');
				break;
		}
		//更新订单状态
		if(!in_array(false, $r)){
			M()->commit();
			$info['status'] =1;
			$info['info'] = '撤销成功';
			return $info;
		}else{
			M()->rollback();
			$info['status'] = -1;
			$info['info'] = '撤销失败';
	
			return $info;
		}
	}
	private function setOrdersStatusByOrdersId($status,$orders_id){
		$r=M('Orders')->where('orders_id = '.$orders_id)->setField('status',$status);
		return $r;
	}
	private function result($member_id,$currency_id){
		$where['member_id']=$member_id;
		$where['type']='sell';
		$r=M('Trade_'.$currency_id)->where($where)->order('add_time asc')->select();		
		$other_num=0;
		$trade_num=0;
		$buy_price=0;
		
 		foreach ($r as $k =>$v){
			$where1['type']='buy';
			$where1['status']=0;
			$where1['member_id']=$member_id;
			$where1['add_time']=array('lt',$v['add_time']);
			$r1 = M('Trade_'.$currency_id)->where($where1)->order('price asc')->select();
			$num=$v['num'];
			$price=$v['price'];
			$profit=0;	
			$money=0;	
			$trade_id=$v['trade_id'];
			if($num>0){
			foreach($r1 as $kk =>$vv){
				if($num>=$vv['num']){
					$profit+=($vv['num']-$other_num)*($price-$vv['price']);
					$num=$num-$vv['num']+$other_num;
					$buy_price+=$vv['price'];
					$trade_num++;
					if($num==0){
						M('Trade_'.$vv['currency_id'])->where('trade_id ='.$trade_id)->setInc('charge',$profit);
					}
					M('Trade_'.$vv['currency_id'])->where('trade_id ='.$vv['trade_id']) ->setField('status',1);
				}else{
					$profit+=$num*($price-$vv['price']);
					$buy_price+=$vv['price'];
					$trade_num++;
					$other_num=$vv['num']-$num;
					$num=0;
					M('Trade_'.$vv['currency_id'])->where('trade_id ='.$trade_id)->setInc('charge',$profit);
				}
			}
		}
			$aaa=$buy_price/$trade_num;
			M('Trade_'.$v['currency_id'])->where('trade_id ='.$trade_id) ->setField('status',1);
			M('Trade_'.$v['currency_id'])->where('trade_id ='.$trade_id) ->setField('buy_price',$aaa);
		/* 	foreach($buy as $kk=>$vv){
				if($num>=$vv['num']){
					
					//$money+=$vv['money'];
					$profit=($vv['num']-$other_num)*($price-$vv['price']);
					$num=$num-$vv['num'];
					$buy_price+=$vv['price']; 
					$trade_num++;
					M('Trade_'.$vv['currency_id'])->where('trade_id ='.$vv['trade_id']) ->setField('status',1);
					M('Trade_'.$vv['currency_id'])->where('trade_id ='.$trade_id)->setInc('charge',$profit);
			     } 
			     if($num>0){
			     
			     	$profit=$num*($price-$vv['price']);
			     	M('Trade_'.$vv['currency_id'])->where('trade_id ='.$trade_id)->setInc('charge',$profit);
			     	//$money+=$num*$price;
			     	$buy_price+=$vv['price'];
			     	$trade_num++;
			     	$other_num=$vv['num']-$num;
			     	$num=0;
			     	//break;
			     }  
			} 
			$aaa=$buy_price/$trade_num;*/
			//$r[$k]['profit']=$trade_num;
			//$r[$k]['profit_bfb']=100*$profit/$money;
			//$r[$k]['buy_price']=$buy_price;
			$trade_num=0;
			$buy_price=0;	
		} 
	}
	public function history(){
		$token=I('token');
		$currency_id=I('currency_id',25);
		$where1['token']=$token;
		$member_id=M('token')->where($where1)->find()['uid'];
		$this->result($member_id,$currency_id);
		$where['member_id']=$member_id;
		$where['type']="sell";
		$r2 = M('Trade_'.$currency_id)->where($where)->order('add_time desc')->select();		
		foreach ($r2 as $kkk=>$vvv){
			$r2[$kkk]['currency_name']=M('Currency')->where('currency_id ='.$vvv['currency_id'])->find()['currency_name'];
			$r2[$kkk]['profit_bfb']=100*($vvv['price']-$vvv['buy_price'])/$vvv['buy_price'];
		}
		$currency_list=M('Currency')->where('is_line = 1')->select();
		$data['currency']=$currency_list;
		$data['list']=$r2;
		$this->ajaxReturn($data,'JSONP'); 
	}
	public function getTradeSql($where){
		$currency=M('Currency')->where('is_line = 1')->select();	
		foreach ($currency as $k =>$v){
			$arr[]=$v['currency_id'];
		}
		for($i=0;$i<count($arr);$i++){
			$sql_union_all .=
			"select * from yang_trade_".$arr[$i]."
			as t".$arr[$i].$where."
			 union all ";
		}
		$sql = substr($sql_union_all,0,-10);
		return $sql;
	}
	// 获取k线
	public function getOrdersKline() {
		if (empty ( $_GET ['currency'] )) {
			return;
		}
		$currency_id = I ( 'get.currency' );
		// K线
		$char = ! empty ( $_GET ['time'] ) ? I ( 'get.time' ) : 'kline_1h';
		switch ($char) {
			case 'kline_5m' :
				$time = 5;
				break;
			case 'kline_15m' :
				$time = 15;
				break;
			case 'kline_30m' :
				$time = 30;
				break;
			case 'kline_1h' :
				$time = 60;
				break;
			case 'kline_8h' :
				$time = 480;
				break;
			case 'kline_1d' :
				$time = 24 * 60;
				break;
			default :
				$time = 60;
		}
		$data [$char] = $this->getKline ( $time, $currency_id );
		$this->ajaxReturn ( $data );
	}
	 //获取K线
	 private function getKline($base_time,$currency_id){
	 $time=time()-$base_time*60*60;
	 for ($i=0;$i<60;$i++){
	 $start= $time+$base_time*60*$i;
	 $end=$start+$base_time*60;
	 //时间
	 $item[$i][]=$start*1000+8*3600*1000;
	 $where['currency_id']=$currency_id;
	 $where['type']='buy';
	 $where['add_time']=array('between',array($start,$end));
	
	// //交易量
	 $num=M('Trade')->where($where)->sum('num');
	 $item[$i][]=!empty($num)?floatval($num):0;
	// //开盘
	 $where_price['currency_id']=$currency_id;
	 $where_price['type']='buy';
      $where_price['add_time']=array('elt',$end);
	
	 $order_k=M('Trade')->field('price')->where($where_price)->order('add_time desc')->find();
	 $item[$i][]=!empty($order_k['price'])?floatval($order_k['price']):0;
	// //最高
	 $max=M('Trade')->where($where)->max('price');
	 $max=!empty($max)?floatval($max):$order_k['price'];
	 $max=!empty($max)?$max:0;
	 $item[$i][]=$max;
	// //最低
	 $min=M('Trade')->where($where)->min('price');
	 $min=!empty($min)?floatval($min):$order_k['price'];
	 $item[$i][]=!empty($min)?$min:0;
	// //收盘
	 $order_s=M('Trade')->field('price')->where($where)->order('add_time asc')->find();
	 $order_s=!empty($order_s['price'])?floatval($order_s['price']):$order_k['price'];
	 $item[$i][]=!empty($order_s)?$order_s:0;
	 }
	 $item=json_encode($item,true);
        return $item;
       }
}